<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科研人员姓名" prop="researcherName">
        <el-input
          v-model="queryParams.researcherName"
          placeholder="请输入科研人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科研人员类型ID" prop="typeId">
        <el-input
          v-model="queryParams.typeId"
          placeholder="请输入科研人员类型ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科研人员职称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入科研人员职称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科研人员邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入科研人员邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科研人员电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入科研人员电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科研人员照片URL或文件路径" prop="photoUrl">
        <el-input
          v-model="queryParams.photoUrl"
          placeholder="请输入科研人员照片URL或文件路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科研人员所属合作单位ID" prop="partnerId">
        <el-input
          v-model="queryParams.partnerId"
          placeholder="请输入科研人员所属合作单位ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除标记：0-未删除，1-已删除" prop="isDeleted">
        <el-input
          v-model="queryParams.isDeleted"
          placeholder="请输入删除标记：0-未删除，1-已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录最后更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入记录创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录更新人" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入记录更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录删除人" prop="deletedBy">
        <el-input
          v-model="queryParams.deletedBy"
          placeholder="请输入记录删除人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录删除时间" prop="deletedAt">
        <el-date-picker clearable
          v-model="queryParams.deletedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录删除时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['peoplegroup:researcherinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['peoplegroup:researcherinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['peoplegroup:researcherinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['peoplegroup:researcherinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="researcherinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键，科研人员ID" align="center" prop="researcherId" />
      <el-table-column label="科研人员姓名" align="center" prop="researcherName" />
      <el-table-column label="科研人员类型ID" align="center" prop="typeId" /v-if="false">
<el-table-column label="科研人员类型" align="center" prop="typeName" />
      
      <el-table-column label="科研人员职称" align="center" prop="title" /v-if="false">
      <el-table-column label="科研人员邮箱" align="center" prop="email" />
      <el-table-column label="科研人员电话" align="center" prop="phone" />
<el-table-column label="科研人员照片" align="center" prop="photoUrl">
  <template slot-scope="scope">
    <!-- 判断 photoUrl 是否存在，存在时显示图片 -->
    <img v-if="scope.row.photoUrl" 
    :src="scope.row.photoUrl" 
    alt="科研人员照片" 
    style="width: 80px; height: 80px; object-fit: cover; cursor: pointer;" 
    @click="handleImageClick(scope.row.photoUrl)"  />
  </template>
</el-table-column>
      <el-table-column label="科研人员所属合作单位ID" align="center" prop="partnerId" /v-if="false">
      
      <el-table-column label="合作单位" align="center" prop="unitName" />
      <el-table-column label="删除标记：0-未删除，1-已删除" align="center" prop="isDeleted" /v-if="false">
      <el-table-column label="记录创建时间" align="center" prop="createdAt" width="180"v-if="false">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录最后更新时间" align="center" prop="updatedAt" width="180"v-if="false">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录创建人" align="center" prop="createdBy" /v-if="false">
      <el-table-column label="记录更新人" align="center" prop="updatedBy" /v-if="false">
      <el-table-column label="记录删除人" align="center" prop="deletedBy" /v-if="false">
      <el-table-column label="记录删除时间" align="center" prop="deletedAt" width="180" v-if="false">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deletedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['peoplegroup:researcherinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['peoplegroup:researcherinfo:remove']"
          >删除</el-button>
    
          <el-button
           size="mini"
           type="text"
           icon="el-icon-document"
           @click="openHistoryDialog(scope.row)"
           v-hasPermi="['peoplegroup:researcherinfo:history']"
          >查看历史</el-button>
         </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  <el-dialog 
  :title="'查看图片'" 
  :visible.sync="imageViewerVisible" 
  width="50%" 
  @close="handleImageClose">
  <!-- 显示当前放大的图片 -->
  <img :src="currentImageUrl" alt="放大图片" style="width: 100%; height: auto; object-fit: contain;" />
</el-dialog>


    <!-- 添加或修改科研人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="科研人员姓名" prop="researcherName">
          <el-input v-model="form.researcherName" placeholder="请输入科研人员姓名" />
        </el-form-item>
        <el-form-item label="科研人员类型ID" prop="researcherTypeId"  v-if ="false">
          <el-input v-model="form.researcherTypeId" placeholder="请输入科研人员类型ID" />
        </el-form-item>

        <el-form-item label="科研人员类型">
             <el-select v-model="form.researcherTypeId" placeholder="请选择科研人员类型">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.researcherTypeId"
                  :label="item.typeName"
                  :value="item.researcherTypeId"
                ></el-option>
              </el-select> 
        </el-form-item>

          <el-form-item label="合作单位">
             <el-select v-model="form.partnerId" placeholder="请选择合作单位">
                <el-option
                  v-for="item in partnerinfoOptions"
                  :key="item.partnerId"
                  :label="item.unitName"
                  :value="item.partnerId"
                ></el-option>
              </el-select> 
         </el-form-item>



        <el-form-item label="科研人员所属合作单位ID" prop="partnerId"  v-if ="false">
          <el-input v-model="form.partnerId" placeholder="请输入科研人员所属合作单位ID" />
        </el-form-item>
        <el-form-item label="科研人员职称" prop="title">
          <el-input v-model="form.title" placeholder="请输入科研人员职称" />
        </el-form-item>
        <el-form-item label="科研人员邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入科研人员邮箱" />
        </el-form-item>
        <el-form-item label="科研人员电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入科研人员电话" />
        </el-form-item>

        
        <el-form-item label="删除标记：0-未删除，1-已删除" prop="isDeleted" v-if ="false">
          <el-input v-model="form.isDeleted" placeholder="请输入删除标记：0-未删除，1-已删除" / >
        </el-form-item>
        <el-form-item label="记录创建时间" prop="createdAt" v-if ="false" >
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录最后更新时间" prop="updatedAt" v-if ="false">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录最后更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录创建人" prop="createdBy" v-if ="false">
          <el-input v-model="form.createdBy" placeholder="请输入记录创建人" />
        </el-form-item>
        <el-form-item label="记录更新人" prop="updatedBy" v-if ="false">
          <el-input v-model="form.updatedBy" placeholder="请输入记录更新人" />
        </el-form-item>
        <el-form-item label="记录删除人" prop="deletedBy" v-if ="false">
          <el-input v-model="form.deletedBy" placeholder="请输入记录删除人" />
        </el-form-item>
        <el-form-item label="记录删除时间" prop="deletedAt" v-if ="false">
          <el-date-picker clearable
            v-model="form.deletedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录删除时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="科研人员照片URL或文件路径" prop="photoUrl">
          <ImageUpload v-model="form.photoUrl" placeholder="请输入科研人员照片URL或文件路径" />
        </el-form-item>

      </el-form>

      <div class="text-center">
            <userAvatar />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  


<el-dialog :title="title" :visible.sync="historyDialogVisible" width="70%" append-to-body>
  <el-table :data="historyData" border style="width: 100%">
    <el-table-column prop="operationType" label="操作类型" align="center" />
    <el-table-column prop="researcherName" label="科研人员姓名" align="center" />
    <el-table-column prop="researcherTypeId" label="科研人员类型ID" align="center" />
    <el-table-column prop="title" label="职称" align="center" />
    <el-table-column prop="email" label="邮箱" align="center" />
    <el-table-column prop="phone" label="电话" align="center" />
    <el-table-column prop="photoUrl" label="照片URL" align="center" />
    <el-table-column prop="partnerId" label="所属合作单位ID" align="center" />
    <el-table-column prop="createdBy" label="创建人" align="center" />
    <el-table-column prop="createdAt" label="创建时间" align="center">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="updatedBy" label="更新人" align="center" />
    <el-table-column prop="updatedAt" label="更新时间" align="center">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="deletedBy" label="删除人" align="center" /v-if="false">
    <el-table-column prop="deletedAt" label="删除时间" align="center" v-if="false">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.deletedAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
      </template>
    </el-table-column>
  </el-table>
  <div slot="footer" class="dialog-footer">
    <el-button @click="historyDialogVisible = false">关闭</el-button>
  </div>
</el-dialog>
</div>




</template>

<script>
import userAvatar from "./userAvatar";
import { listResearcherinfo, getResearcherinfo, delResearcherinfo, addResearcherinfo, updateResearcherinfo } from "@/api/peoplegroup/researcherinfo";
import{getAllresearchertype} from "@/api/peoplegroup/researchertype";
import{getAllPartnerinfo} from "@/api/peoplegroup/partnerinfo";
import{getresearcherhistoryInfo} from "@/api/peoplegroup/researcherhistory";
export default {
  name: "Researcherinfo",
  components: {userAvatar},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
// 当前放大的图片URL
      currentImageUrl: '',
// 控制图片查看弹窗的显示
      imageViewerVisible: false, // 新增属性，控制图片查看框的显示

      historyDialogVisible: false, // 控制对话框显示
      // 总条数
      total: 0,
      // 科研人员信息表格数据
      researcherinfoList: [],

      typeOptions:[],

      partnerinfoOptions: [],

      historyData: [], // 存储历史记录数据
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        researcherName: null,
        typeId: null,
        title: null,
        email: null,
        phone: null,
        photoUrl: null,
        partnerId: null,
        isDeleted: null,
        createdAt: null,
        updatedAt: null,
        createdBy: null,
        updatedBy: null,
        deletedBy: null,
        deletedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        researcherName: [
          { required: true, message: "科研人员姓名不能为空", trigger: "blur" }
        ],
        researcherTypeId: [
          { required: true, message: "科研人员类型ID不能为空", trigger: "blur" }
        ],

        partnerId: [
          { required: true, message: "科研人员所属合作单位ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },

watch:{
  '$store.getters.avatar':function(newAvatar){
    this.form.photoUrl=newAvatar;
  }
},
  methods: {
    /** 查询科研人员信息列表 */
    getList() {
      this.loading = true;
      listResearcherinfo(this.queryParams).then(response => {
        this.researcherinfoList = response.rows.filter(item => item.isDeleted !== 1);
        console.log(response);
        this.total = response.total;
        this.loading = false;
      });
    },


    
      //查看历史记录
    openHistoryDialog(row) {
      const researcherId = row.researcherId;
      if (!researcherId) {
        this.$message.error('参数无效');
        return;
      }
     



getresearcherhistoryInfo(researcherId)
        .then(response => {
          // 检查数据是否是数组
          if (Array.isArray(response.data)) {
            this.historyDialogVisible = true;
            this.historyData = response.data; // 直接使用数组
          } else {
            // 如果是对象，包裹成数组
            this.historyDialogVisible = true;
            this.historyData = [response.data]; 
          }
          console.log('历史记录数据:', this.historyData);  // 打印数据以检查
        })
        .catch(error => {
          console.error('获取历史记录失败:', error);
        });



    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        researcherId: null,
        researcherName: null,
        typeId: null,
        title: null,
        email: null,
        phone: null,
        photoUrl: null,
        partnerId: null,
        isDeleted: null,
        createdAt: null,
        updatedAt: null,
        createdBy: null,
        updatedBy: null,
        deletedBy: null,
        deletedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.researcherId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
   
     handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加科研人员信息";

  // 获取所有科研人员类型
     getAllresearchertype().then(response => {
         console.log(response);
         this.typeOptions = response.data;
         }).catch(error => {
         console.error("获取科研人员类型失败:", error);
      });

  // 获取所有合作单位信息
     getAllPartnerinfo().then(response => {
         console.log(response);
         this.partnerinfoOptions = response.data;
         }).catch(error => {
          console.error("获取合作单位信息失败:", error);
        });
     },
        



     

/** 新增下拉框选项操作 */
      addDropdownOption(optionType, newOption) {
      if (optionType === 'typeOptions') {
      this.typeOptions.push(newOption);
     } else if (optionType === 'partnerinfoOptions') {
       this.partnerinfoOptions.push(newOption);
     } else {
    console.error("未知的下拉框类型:", optionType);
  }
},

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const researcherId = row.researcherId || this.ids
      getResearcherinfo(researcherId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改科研人员信息";
        store.commit('SET_AVATAR',this.form.photoUrl);
        console.log(store.getters.avatar);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.researcherId != null) {
            updateResearcherinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResearcherinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const researcherIds = row.researcherId || this.ids;
      this.$modal.confirm('是否确认删除科研人员信息编号为"' + researcherIds + '"的数据项？').then(function() {
        return delResearcherinfo(researcherIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('peoplegroup/researcherinfo/export', {
        ...this.queryParams
      }, `researcherinfo_${new Date().getTime()}.xlsx`)
    },


    handleImageClick(photoUrl) {
      this.currentImageUrl = photoUrl;
      this.imageViewerVisible = true;
    },
    handleImageClose() {
      this.currentImageUrl = '';
      this.imageViewerVisible = false;
    }
  }
  };

</script>
